package data;

import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.Serializable;
import java.util.ArrayList;

import data.Element.Company;
import data.Element.Doctype;
import data.Element.Nationality;
import data.Element.Year;


public class Database implements Serializable{
	ArrayList<Element> element = new ArrayList<Element>();
	
	//operations here
	public String search(String s, ArrayList<Option> options) {
		ArrayList<Element>  limited = new ArrayList<Element>();
		for (Element e: element) {
			boolean satisfies = true;
			for (Option o: options){
				if (!e.satisfiesOption(o))
					satisfies = false;
			}
			if (satisfies) limited.add(e);
		}
		
		ArrayList<Element> complete = new ArrayList<Element>();
		s = s.toUpperCase();
		for (Element e:limited) {
			String eName =  e.getName().toUpperCase();
			if (e.getName().indexOf(s) != -1) {
				complete.add(e);
			}
		}
		String output = "";
		for (Element e:complete){
			output += e + "$";
		}
		
		return output;
	}
	
	public void testSetup() {
		for (int i=0; i<30; i++) {
			int r;
			Nationality n = null;
			Year y = null;
			Company c = null;
			Doctype d = null;
			
			r = (int)(Math.random()*2);
			if (r == 0) n = Nationality.NA; else if (r == 1) n = Nationality.EU;
			
			r = (int)(Math.random()*3);
			if (r == 0) y = Year.y2010; else if (r == 1) y = Year.y2011; else if (r==2) y = Year.y2012;
			
			r = (int)(Math.random()*2);
			if (r == 0) c = Company.GE; else if (r == 1) c = Company.Oracle;
			
			r = (int)(Math.random()*2);
			if (r == 0) d = Doctype.MRI; else if (r == 1) d = Doctype.DISCHARGE;
			
			element.add(new Element("Name"+i, n, y, c, d));
		}
	}

	public int getSize() {
		return element.size();
	}
}
